#include "../include/threadPool.h"


int main(int argc, char *argv[]){                                  
    MYSQL *mysql;
    /* pthread_mutex_lock(&threadPool.mutex); */
    mysqlConnect(&mysql);
    /* pthread_mutex_unlock(&threadPool.mutex); */
    threadPool_t threadPool;
    threadPoolInit(&threadPool,5);
    int sockfd = tcpInit("192.168.201.130","12345");
    int epfd = epoll_create(1);
    epollAdd(epfd,sockfd);
    makeWorker(&threadPool);
    struct epoll_event readyset[1024];
    circular_queue_t cirQueue;
    circularQueueInit(&cirQueue);
    int curidx = 0;
    while(1){
        int readyNum = epoll_wait(epfd,readyset,1024,1000);
        for(int i = 0; i < readyNum; i++){
            if(readyset[i].data.fd == sockfd){
                int netfd = accept(sockfd,NULL,NULL);
                //-------------------------------------------
                //服务端收命令类型
                int isConnect;
                recv(netfd, &isConnect, sizeof(int), MSG_WAITALL);
                //  此时为第一次连接
                if(isConnect == 0){
                    char token[1024] = {0};
                    // new connect 
                    char prompt[1024] = {0};
                    char input[1024] = {0};
                    /* pthread_mutex_lock(&threadPool.mutex); */
                    /* mysqlConnect(&mysql); */
                    /* pthread_mutex_unlock(&threadPool.mutex); */
                    int isLogin; // 1 for login, 0 for register
                                 // 询问是登录还是注册
                    strcpy(prompt, "请选择: 1.登录 2.注册\n>>:");
                    send(netfd, prompt, strlen(prompt), 0);
                    recv(netfd, input, sizeof(input), 0);
                    isLogin = (atoi(input) == 1) ? 1 : 0;
                    bzero(input, sizeof(input));

                    // 获取用户名
                    bzero(prompt,sizeof(prompt));
                    strcpy(prompt, "用户名:");
                    printf("user:%s\n",prompt);
                    send(netfd, prompt, strlen(prompt), 0);
                    recv(netfd, input, sizeof(input), 0);
                    char username[256];
                    strncpy(username, input, sizeof(username));
                    bzero(input, sizeof(input));

                    // 获取密码
                    bzero(prompt,sizeof(prompt));
                    strcpy(prompt, "密码:");
                    printf("psw:%s\n",prompt);
                    send(netfd, prompt, strlen(prompt), 0);
                    recv(netfd, input, sizeof(input), 0);
                    char password[256];
                    strncpy(password, input, sizeof(password));
                    if(isLogin == 1){
                        if(logIn(mysql,username,password) != 0){
                            char msg3[1024] = "密码错误，登录失败!\n";
                            send(netfd,msg3,strlen(msg3),MSG_NOSIGNAL);
                            close(netfd);
                        }else{
                            // send logon msg
                            char msg4[1024] = "密码正确，登陆成功!\n";
                            send(netfd,msg4,strlen(msg4),MSG_NOSIGNAL);
                            // send pathId to client
                            printf("--------------\n");
                            int pathId = getUserRootPathIdByUsername(mysql, username);
                            printf("%d\n",pathId);
                            printf("--------------\n");
                            send(netfd, &pathId, sizeof(int), MSG_NOSIGNAL);
                            printf("--------------\n");
                            // send token to client
                            getToken(username, token, SECRET);
                            printf("get token = %s",token);
                            /* sendToken(netfd, token); */
                            int tokenLen = strlen(token)+1;
                            send(netfd,&tokenLen,sizeof(int),0);
                            send(netfd,token,tokenLen,0);

                            //
                            //
                            //
                            epollAdd(epfd, netfd);
                        }

                    }else{
                        if(Register(mysql,username,password) != 0){
                            char msg5[1024] = "用户已存在，注册失败!\n";
                            send(netfd,msg5,strlen(msg5),MSG_NOSIGNAL);
                            close(netfd);
                        }else{
                            //char msg6[1024] = "注册成功!\n";
                            /* send(netfd,msg6,strlen(msg6),MSG_NOSIGNAL); */
                            // and send root pathId to client


                            //  手动实现注册成功后自动登录
                            logIn(mysql,username,password);
                            char msg7[1024] = "注册并登录成功!\n";

                            send(netfd,msg7,strlen(msg7),MSG_NOSIGNAL);
                            int pathId = getUserRootPathIdByUsername(mysql, username);
                            send(netfd, &pathId, sizeof(int), MSG_NOSIGNAL);
                            getToken(username,token,SECRET);
                            sendToken(netfd, token);
                            epollAdd(epfd, netfd);

                        }
                    }
                }else{
                    pthread_mutex_lock(&threadPool.mutex);
                    enQueue(&threadPool.taskQueue,netfd);
                    pthread_cond_broadcast(&threadPool.cond);
                    pthread_mutex_unlock(&threadPool.mutex);
                }
            }else{
                //  此时收到的是已经建立连接的客户端主进程发来的短命令消息
                char phyPath[1024] = "/home/playba/gitLibrary/no.4/server/src/FILE/";
                char buf[4096] = {0};
                char result[4096] = {0};
                int cmd;
                char *filePath = (char *)calloc(8192, sizeof(char));
                strcpy(filePath,phyPath);
                int pathId;
                int shortFd = readyset[i].data.fd;
                recvCmd_p(shortFd, buf, &cmd, &pathId);
                addClient(&cirQueue,shortFd);
                switch(cmd){
                case 0:
                    close(shortFd);
                    break;
                case CD:
                    my_cd(mysql,buf,&pathId,result);
                    sendCmd_p(shortFd,result,CD,&pathId);
                    break;
                case LS:
                    my_ls(mysql,buf,pathId,result);
                    sendCmd_p(shortFd,result,LS,&pathId); 
                    break;
                case PWD:
                    my_pwd(mysql,pathId,result);
                    sendCmd_p(shortFd,result,PWD,&pathId);
                    break;
                    //case PUTS:
                    //    break;
                    //case GETS:
                    //    break;
                case MKDIR:
                    my_mkdir(mysql,buf,pathId,result);
                    sendCmd_p(shortFd,result,MKDIR,&pathId);
                    break;
                case RMDIR:
                    removeDir(mysql,buf,pathId,result);
                    sendCmd_p(shortFd,result,RMDIR,&pathId);
                    break;
                case REMOVE:
                    removeFile(mysql,buf,pathId,result,phyPath);
                    sendCmd_p(shortFd,result,REMOVE,&pathId);
                    break;
                }
            }
        }
        /* // 超时踢人 */
        /* superCheck( &cirQueue); */
        /* for(int i = 0; i < readyNum; i++){ */
        /*     addClient(&cirQueue,readyset[i].data.fd); */
        /* } */
        /* tick(&cirQueue); */
    }
    return 0;
}
